<?php 
/**
 * 鸡蛋美术 活动版块
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/lib_order.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}
assign_template();

if (!isset($_REQUEST['act']))
{
    $_REQUEST['act'] = "";
    if(!empty($_REQUEST['id'])){
        $_REQUEST['act'] = "info";
    }
}

/* 礼包详情 */
if($_REQUEST['act'] == 'info'){
    // 设置所在地区
    $area_id = !empty($_SESSION['area_id'])?$_SESSION['area_id']:3;
    if(!empty($_REQUEST['id'])){
        $parent_id = intval($_REQUEST['id']);
    }else{
        /* 如果没有找到任何记录则跳回到首页 */
        ecs_header("Location: http://www.eggms.com/errpage/goods.php\n");
        exit;
    }
    /* 获取礼包信息 */
    $sql = "SELECT * FROM " . $ecs->table('activity') . " WHERE parent_id = '$parent_id'";
    $info = $db->getRow($sql);
    if(!$info){
        /* 如果没有找到任何记录则跳回到首页 */
        ecs_header("Location: http://www.eggms.com/errpage/goods.php\n");
        exit;
    }

    /* 获取商品信息 */
    $sql = "SELECT ag.id, g.goods_name, ga.attr_value, ag.goods_number,g.goods_thumb,g.goods_img, g.original_img FROM " . $ecs->table('activity_goods') . " ag LEFT JOIN " . $ecs->table('goods') . " g ON g.goods_id = ag.goods_id LEFT JOIN " . $ecs->table('goods_attr') . " ga ON ga.goods_attr_id = ag.goods_attr_id WHERE ag.parent_id = '$parent_id'";
    $info['goods_list'] = $db->getAll($sql);

    // 获取礼包发放信息
    $sql = "SELECT * FROM " . $ecs->table('activity_stock') . " acts LEFT JOIN ".$ecs->table('service_area')." sa ON sa.area_id = acts.area_id WHERE acts.parent_id = $parent_id AND acts.area_id = $area_id";
    $info['stock_list'] = $db->getRow($sql);
    // 计算销量
    $info['beiqiang'] = intval($info['stock_list']['total_number'] - $info['stock_list']['valid_number']);
    // 计算折扣
    $info['discount'] = intval(($info['price'])*10/$info['original_price']);
    // 分享到微博链接
    $info['share_url'] = "http://service.weibo.com/share/share.php?url=".urlencode("http://www.eggms.com/activity.php?id=".$info['parent_id'])."&type=button&ralateUid=2809270770&language=zh_cn&title=".urlencode($info['name'])."&pic=".urlencode('http://www.eggms.com/'.$info['img'])."&searchPic=false&style=simple";
    // 格式化手机版数量选择
    for ($i=1; $i <= $info['stock_list']['valid_number']; $i++) {
        $stock[$i]=$i;
    }
    $smarty->assign('stock', $stock);
    $smarty->assign('info', $info);
    $smarty->assign('top_title', '劲惠礼包');
    $smarty->display('activityInfo.dwt');
}
/* 礼包版块列表 */
elseif($_REQUEST['act'] == ''){
    // 清空购物车里的超时礼包
    clearActivityCart();
    // 设置所在地区
    $area_id = !empty($_SESSION['area_id'])?$_SESSION['area_id']:3;
    //获取礼包列表
    $sql = "SELECT * FROM " . $ecs->table('activity') . " WHERE is_sale = 1 AND extension_code = 'libao' ORDER BY parent_id DESC ";
    $list = $db->getAll($sql);
    foreach ($list as $key => $value) {
        $list[$key]['add_time'] = date("Y-m-d H:i:s", $value['add_time']);
        $list[$key]['update_time'] = date("Y-m-d H:i:s", $value['update_time']);
        // 获取礼包商品信息
        $sql = "SELECT ag.id, g.goods_name, ga.attr_value, ag.goods_number FROM " . $ecs->table('activity_goods') . " ag LEFT JOIN " . $ecs->table('goods') . " g ON g.goods_id = ag.goods_id LEFT JOIN " . $ecs->table('goods_attr') . " ga ON ga.goods_attr_id = ag.goods_attr_id WHERE ag.parent_id = '$value[parent_id]'";
        $list[$key]['goods_list'] = $db->getAll($sql);
        // 获取礼包发放信息
        $sql = "SELECT * FROM " . $ecs->table('activity_stock') . " acts LEFT JOIN ".$ecs->table('service_area')." sa ON sa.area_id = acts.area_id WHERE acts.parent_id = $value[parent_id] AND acts.area_id = $area_id";
        $list[$key]['stock_list'] = $db->getRow($sql);
        $list[$key]['key'] = $key%2;
        // 计算折扣
        $list[$key]['discount'] = intval(($value['price'])*10/$value['original_price']);
        // 计算销量
        $list[$key]['beiqiang'] = intval($list[$key]['stock_list']['total_number'] - $list[$key]['stock_list']['valid_number']);
    }
    $smarty->assign('list', $list);
    $smarty->assign('top_title', '劲惠礼包');
    $smarty->display('activity.dwt');
}


 ?>